package com.aibaixun.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.aibaixun.system.domain.DicmCabinet;
import com.aibaixun.system.domain.PostureDeviceNew;
import com.aibaixun.system.domain.dto.DicmPostureDeviceDto;
import com.aibaixun.system.domain.dto.PostureChildrenDto;
import com.aibaixun.system.mapper.PostureDeviceNewMapper;
import com.aibaixun.system.service.PostureDeviceNewService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <b>PostureDeviceNewServiceImpl</b>
 *
 * @description: PostureDeviceNewServiceImpl <br>
 * @date: 2024/8/29 14:16 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Service
public class PostureDeviceNewServiceImpl extends ServiceImpl<PostureDeviceNewMapper, PostureDeviceNew> implements PostureDeviceNewService {
    private static final Logger logger = LoggerFactory.getLogger(PostureDeviceNewServiceImpl.class);


    @Override
    public void savePostureDevice(List<DicmPostureDeviceDto> postureList,String cabinetId) {
        List<PostureDeviceNew> postureDeviceList = new ArrayList<>();
        for (DicmPostureDeviceDto dicmPostureDeviceDto : postureList) {
            if (CollectionUtil.isNotEmpty(dicmPostureDeviceDto.getChildren())){
                List<PostureChildrenDto> children = dicmPostureDeviceDto.getChildren();
                //根据机柜名称查询机柜信息
                DicmCabinet dicmCabinet = baseMapper.queryCabinetId(cabinetId,dicmPostureDeviceDto.getSeatCode());
                children.forEach(item -> {
                    PostureDeviceNew postureDeviceNew = baseMapper.queryPostureDeviceByDeviceId(item.getSmallDeviceId());
                    if (null == postureDeviceNew) {
                        PostureDeviceNew postureDevice = new PostureDeviceNew();
                        postureDevice.setDeviceId(item.getSmallDeviceId());
                        postureDevice.setName(item.getDeviceType());
                        postureDevice.setCabinetNum(dicmPostureDeviceDto.getSeatCode());
                        postureDevice.setModel(item.getDeviceModel());
                        postureDevice.setLocation(Integer.parseInt(item.getUstate()));
                        postureDevice.setCreateTime(LocalDateTime.now());
                        postureDevice.setUpdateTime(LocalDateTime.now());
                        postureDevice.setHeight(Integer.parseInt(item.getHeight()));
                        if (dicmCabinet != null && StringUtils.isNotEmpty(dicmCabinet.getUeId())) {
                            postureDevice.setUeRelation(dicmCabinet.getUeId() + "_u_" + postureDevice.getLocation());
                            postureDevice.setCabinetAddressId(dicmCabinet.getAddressId());
                            postureDevice.setCabinetUeid(dicmCabinet.getUeId());
                        }
                        postureDevice.setUuse(Integer.parseInt(dicmPostureDeviceDto.getUuse()));
                        postureDevice.setUsum(Integer.parseInt(dicmPostureDeviceDto.getUsum()));
                        postureDeviceList.add(postureDevice);
                    }
                });
            }
        }
        this.saveBatch(postureDeviceList);
    }
}
